﻿using System;
using System.Reflection;
using NetMigration.Factories;
using NetMigration.Utilities;

namespace NetMigrate
{
    public class Program
    {
        public static void Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("Usage: NetMigrate yourAssembly.dll ConnectionString [option: version=xxx]");
                return;
            }
            try
            {
                var migrationExecuter = new MigrationExecuter(new SQLServerMigrationFactory(args[1]));
                if (args.Length == 3)
                {
                    Console.WriteLine("roll back to version");
                    int version = int.Parse(args[2].Split('=')[1]);
                    migrationExecuter.Migrate(Assembly.LoadFrom(args[0]), version);
                }
                else
                {
                    migrationExecuter.Migrate(Assembly.LoadFrom(args[0]));
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(string.Format("Can't load assembly {0}", args[0]));
                Console.WriteLine(e);
            }
        }
    }
}